/*
 * @Author: shijianxin
 * @Date: 2022-04-22 16:40:56
 * @Description: 电影的action
 */

import "redux";
import { ThunkAction } from "redux-thunk";
import { ISearchCondition, SwitchType } from "../../services/CommonType";
import { IMovie, MovieService } from "../../services/MovieService";
import { IRootState } from "../reducers/RootReducer";
import { IAction } from "./ActionTypes";

export type SaveMoviesAction = IAction<
  "movie_save",
  {
    movies: IMovie[];
    total: number;
  }
>;

export function saveMoviesAction(
  movies: IMovie[],
  total: number
): SaveMoviesAction {
  return {
    type: "movie_save",
    payload: {
      movies,
      total,
    },
  };
}

export type SetLoadingAction = IAction<"movie_setLoading", boolean>;

function setLoadingAction(isLoading: boolean): SetLoadingAction {
  return {
    type: "movie_setLoading",
    payload: isLoading,
  };
}

export type SetConditionAction = IAction<
  "movie_setCondition",
  ISearchCondition
>;

function SetConditionAction(condition: ISearchCondition): SetConditionAction {
  return {
    type: "movie_setCondition",
    payload: condition,
  };
}

export type SetDeleteAction = IAction<"movie_delete", string>;

function deleteAction(id: string): SetDeleteAction {
  return {
    type: "movie_delete",
    payload: id,
  };
}

export type MovieActions =
  | SaveMoviesAction
  | SetConditionAction
  | SetLoadingAction
  | SetDeleteAction
  | MovieChangeSwitchAction;

function fetchMovies(
  condition: ISearchCondition
): ThunkAction<Promise<void>, IRootState, any, MovieActions> {
  return async (dispatch, getState) => {
    dispatch(setLoadingAction(true));
    dispatch(SetConditionAction(condition));
    console.log(getState());
    const curCondition = getState().movie.condition;
    const resp = await MovieService.getMovies(curCondition);
    dispatch(saveMoviesAction(resp.data, resp.total));
    dispatch(setLoadingAction(false));
  };
}

function deleteMovie(
  id: string
): ThunkAction<Promise<void>, IRootState, any, MovieActions> {
  return async (dispath) => {
    dispath(setLoadingAction(true));
    await MovieService.delete(id);
    dispath(deleteAction(id));
    dispath(setLoadingAction(false));
  };
}

export type MovieChangeSwitchAction = IAction<
  "movie_switch",
  {
    type: SwitchType;
    newVal: boolean;
    id: string;
  }
>;

function changeSwitchAction(
  type: SwitchType,
  newVal: boolean,
  id: string
): MovieChangeSwitchAction {
  return {
    type: "movie_switch",
    payload: {
      type,
      newVal,
      id,
    },
  };
}

function changeSwitch(
  type: SwitchType,
  newVal: boolean,
  id: string
): ThunkAction<Promise<void>, IRootState, any, MovieActions> {
  return async (dispatch) => {
    dispatch(changeSwitchAction(type, newVal, id));
    await MovieService.edit(id, {
      [type]: newVal,
    });
  };
}

export default {
  saveMoviesAction,
  setLoadingAction,
  SetConditionAction,
  deleteAction,
  fetchMovies,
  deleteMovie,
  changeSwitchAction,
  changeSwitch,
};
